﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;

namespace JoyfulHomeJourney.BackEnd.Infrastructure.Tools
{
    public static class Tools
    {
        public static string Join(List<string> strings, string concat)
        {
            if (strings.Count == 0)
            {
                return "";
            }
            string result = "";
            foreach (string s in strings)
            {
                result += s + concat;
            }
            return result.Substring(0, result.Length - concat.Length);
        }
        public static string Guid_1()
        {
            return Guid.NewGuid().ToString().Replace("-", "");
        }
        public static string Guid_2()
        {
            return DateTimeOffset.UtcNow.ToUnixTimeSeconds() + Guid.NewGuid().ToString().Replace("-", "").Substring(10);
        }
        public static string GetMainProgramPath()
        {
            return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "..", "..", "..");
        }
        
        public static string GetImageCarPath()
        {
            return Path.Combine(GetMainProgramPath(),"wwwroot","Images","Cars");
        }
        public static string GetImageCarShopShopPath()
        {
            return Path.Combine(GetMainProgramPath(), "wwwroot", "Images", "CarShop","Shop");
        }
        public static string GetImageCarShopCertificatePath()
        {
            return Path.Combine(GetMainProgramPath(), "wwwroot", "Images", "CarShop","Certificate");
        }
        public static string GetImageProductPath()
        {
            return Path.Combine(GetMainProgramPath(),"wwwroot","Images","Product");
        }
        public static string GetImageCarSuppliesPath()
        {
            return Path.Combine(GetMainProgramPath(), "wwwroot", "Images","Product", "CarSupplies");
        }

        public static string GetImageCarServicePath()
        {
            return Path.Combine(GetMainProgramPath(),"wwwroot","Images","Product","CarServices");
        } 

        // 删除带文件的目录
        public static void DeleteDirectory(string targetDir)
        {
            if (Directory.Exists(targetDir))
            {
                // 获取目录中的所有文件
                foreach (string file in Directory.GetFiles(targetDir))
                {
                    File.Delete(file); // 删除文件
                }

                // 获取目录中的所有子目录
                foreach (string directory in Directory.GetDirectories(targetDir))
                {
                    DeleteDirectory(directory); // 递归删除子目录
                }

                // 删除目录本身
                Directory.Delete(targetDir);
            }
        }
        public static void DeleteImage(string path)
        {
            // 检查文件是否存在
            if (System.IO.File.Exists(path))
            {
                // 删除文件
                System.IO.File.Delete(path);
            }
        }
        private const long _maxImageFileSize = 4 * 1024 * 1024; // 4MB
        private static readonly List<string> _allowedImageExtensions = new List<string> { ".jpg", ".jpeg", ".png", ".gif" };
        private static readonly List<string> _allowedImageContentTypes = new List<string> { "image/jpeg", "image/png", "image/gif" };
        private static bool IsFileSizeValid(IFormFile file, long maxSize)//2 * 1024 * 1024 2mb
        {
            return file.Length <= maxSize;
        }
        private static bool IsFileTypeValid(IFormFile file, List<string> allowedExtensions, List<string> allowedContentTypes)
        {
            var extension = Path.GetExtension(file.FileName).ToLowerInvariant();
            var contentType = file.ContentType.ToLowerInvariant();
            return allowedExtensions.Contains(extension) && allowedContentTypes.Contains(contentType);
        }
        public static async Task<string> UploadImage(IFormFile imageFile, string pathDir)
        {
            if (!IsFileSizeValid(imageFile, _maxImageFileSize))
            {
                return null;
            }
            if (!IsFileTypeValid(imageFile, _allowedImageExtensions, _allowedImageContentTypes))
            {
                return null;
            }
            return await Upload(imageFile, pathDir);
        }
        private static async Task<string> Upload(IFormFile file, string pathDir)
        {
            var fileName = Guid_1() + Path.GetExtension(file.FileName).ToLowerInvariant();

            // 确保upload的文件夹存在
            if (!Directory.Exists(pathDir))
            {
                Directory.CreateDirectory(pathDir);
            }

            using (var stream = new FileStream(Path.Combine(pathDir, fileName), FileMode.Create))
            {
                await file.CopyToAsync(stream);
            }

            return fileName;
        }

        public static string TimeToString(DateTime? time, string split1 = "-", string split2 = ":")
        {
            if (time == null)
            {
                return "";
            }
            DateTime t = (DateTime)time;
            return GT(t.Year,4) + split1 + GT(t.Month) + split1 + GT(t.Day) + " " + GT(t.Hour) + split2 + GT(t.Minute) + split2 + GT(t.Second)
                ;
        }
        // 获取时间部分数字的正确显示
        private static string GT(int partTime,int length=2)
        {
            string result = "";
            for (int i = 0; i < length-partTime.ToString().Length; i++)
            {
                result += "0";
            }
            result += partTime.ToString();
            return result;
        }
    }
}
